using System;
using System.Collections.Generic;
using System.Text;

namespace TracClient
{
  public class Ticket
  {
    //Dictionary<string, string> val = new Dictionary<string, string>();
    List<string> keys = new List<string>();
    List<string> values = new List<string>();

    public Ticket()
    {
    }

    public Ticket(string fields, string values_)
    {
      string[] f = fields.Split(",".ToCharArray());
      string[] v = ReplaceComma(values_, '`').Split(",".ToCharArray());
      for (int i = 0; i < Math.Min(f.Length, v.Length); ++i)
      {
        //val.Add(f[i], v[i].Replace("`", ","));
        keys.Add(f[i]);
        values.Add(v[i].Replace("`", ","));
      }
    }

    public string GetValueRaw(string field)
    {
      int i = keys.IndexOf(field);
      if (i == -1)
      //if (!val.ContainsKey(field))
        return string.Empty;
      //return val[field];
      return values[i];
    }

    public string GetValue(string field)
    {
      string s = GetValueRaw(field);
      DateTime date;
      if (DateTime.TryParse(s, out date))
      {
        TimeSpan elapsed = (DateTime.Now - date);
        if (elapsed.Days > 30)
          s = string.Format("{0} month{1} ago", (elapsed.Days / 30), (elapsed.Days / 30) > 1 ? "s" : "");
        else if (elapsed.Days != 0)
          s = string.Format("{0} day{1} ago", elapsed.Days, elapsed.Days > 1 ? "s" : "");
        else if (elapsed.Hours != 0)
          s = string.Format("{0} hour{1} ago", elapsed.Hours, elapsed.Hours > 1 ? "s" : "");
        else
          s = string.Format("{0} minute{1} ago", elapsed.Minutes, elapsed.Minutes > 1 ? "s" : "");
      }
      if (s.StartsWith("\""))
        s = s.Substring(1);
      if (s.EndsWith("\""))
        s = s.Remove(s.Length - 1);
      return s;
    }

    string ReplaceComma(string s, char c)
    {
      string ret = string.Empty;
      bool inString = false;
      for (int i = 0; i < s.Length; ++i)
      {
        if (inString && s[i] == ',')
          ret += c;
        else
        {
          if (s[i] == '"')
            inString = !inString;
          ret += s[i];
        }
      }
      return ret;
    }

    public bool Contains(string text)
    {
      if (details.Length > 0 && details.IndexOf(text, StringComparison.OrdinalIgnoreCase) != -1)
        return true;
      for (int i = 0; i < values.Count; ++i)
        if (values[i].IndexOf(text, StringComparison.OrdinalIgnoreCase) != -1)
          return true;
      return false;
    }

    private bool unread = false;
    public bool Unread
    {
      get { return unread; }
      set { unread = value; }
    }

    private string details = string.Empty;
    public string Details
    {
      get { return details; }
      set { details = value; }
    }

    public List<string> Keys
    {
      get { return keys; }
      set { keys = value; }
    }

    public List<string> Values
    {
      get { return values; }
      set { values = value; }
    }
  }
}
